﻿import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Extras 1.4

/****************** 动态菜单项 *********************

1、功能
    可作为菜单中的菜单项，支持四种类型的样式:Text(纯文本)、Icon（图标+文本）、Check（复选框+文本）、Radio（单选框+文本）。

2、用法
（1）设置mode属性，选择工作模式
（2）设置parentAdMenu属性，指定父菜单。此属性用于从父菜单获取颜色参数。
（4）根据不同的工作模式设置菜单项的初始状态：text（文本）、iconUrl（图标）、checked（单选和复选状态）、buttonGroup（单选所属按钮）
（5）处理triggerd()信号，该信号将在菜单项被点击时发出。对于Check和Radio模式，还可以在此信号中访问checked属性获取选中状态。

****************** 动态菜单项 *********************/

MenuItem{
    id:controlRoot

    enum Mode{
        Text,
        Icon,
        Check,
        Radio
    }
    property int          mode: AdMenuItem.Text
    property AdMenu       parentAdMenu
    property ButtonGroup  buttonGroup
    property url          iconUrl:""

    enabled: true
    checkable: true
    width: parentAdMenu.itemWidth
    height: parentAdMenu.itemHeight
    background: Item {}
    indicator: Item{}
    contentItem:Loader{
        anchors.fill: parent
        enabled: true
        sourceComponent: controlRoot.mode===AdMenuItem.Text?textComponent:
                         controlRoot.mode===AdMenuItem.Icon?iconComponent:
                         controlRoot.mode===AdMenuItem.Check?checkComponent:
                         controlRoot.mode===AdMenuItem.Radio?raidoComponent:undefined
    }

    Component{     //文本类型
        id:textComponent
        Rectangle{
            color: controlRoot.enabled?(textMouseArea.containsMouse?parentAdMenu.itemHoveredBackColor:parentAdMenu.itemNormalBackColor):parentAdMenu.itemDisabledBackColor
            border.color:controlRoot.enabled?(textMouseArea.containsMouse?parentAdMenu.itemHoveredBorderColor:parentAdMenu.itemNormalBorderColor):parentAdMenu.itemDisabledBorderColor
            Text {
                anchors.fill: parent
                text: controlRoot.text
                font.pixelSize: controlRoot.height*0.5
                leftPadding:controlRoot.height*0.7+15
                color:  controlRoot.enabled?(textMouseArea.containsMouse?parentAdMenu.itemHoveredTextColor:parentAdMenu.itemNormalTextColor):parentAdMenu.itemDisabledTextColor
                verticalAlignment: Qt.AlignVCenter
                textFormat: Text.RichText
            }
            MouseArea{
                id:textMouseArea
                enabled: true
                hoverEnabled: true
                anchors.fill: parent
                propagateComposedEvents :true
                onPressed: {
                    triggered()
                    mouse.accepted=false                    
                }
            }
        }
    }

    Component{    //图标类型
        id:iconComponent
        Rectangle{
            color: controlRoot.enabled?(iconMouseArea.containsMouse?parentAdMenu.itemHoveredBackColor:parentAdMenu.itemNormalBackColor):parentAdMenu.itemDisabledBackColor
            border.color:controlRoot.enabled?(iconMouseArea.containsMouse?parentAdMenu.itemHoveredBorderColor:parentAdMenu.itemNormalBorderColor):parentAdMenu.itemDisabledBorderColor
            Button{
                id:icon
                enabled: false
                width:controlRoot.height*0.7
                height: controlRoot.height*0.7
                icon.source:controlRoot.iconUrl
                icon.color: controlRoot.enabled?(iconMouseArea.containsMouse?parentAdMenu.itemHoveredIndicatorColor:parentAdMenu.itemNormalIndicatorColor):parentAdMenu.itemDisabledIndicatorColor
                icon.height: parent.height
                icon.width: parent.width
                display: Button.IconOnly
                background: Item{}
                anchors.left: parent.left
                anchors.leftMargin: 5
                anchors.verticalCenter: parent.verticalCenter
            }
            Text{
                anchors.left: icon.right
                anchors.leftMargin: 10
                width: parent.width-icon.width-15
                height: parent.height
                text: controlRoot.text
                color:  controlRoot.enabled?(iconMouseArea.containsMouse?parentAdMenu.itemHoveredTextColor:parentAdMenu.itemNormalTextColor):parentAdMenu.itemDisabledTextColor
                verticalAlignment: Qt.AlignVCenter
                font.pixelSize: parent.height/2
                textFormat: Text.RichText
            }
            MouseArea{
                id:iconMouseArea
                enabled: true
                hoverEnabled: true
                anchors.fill: parent
                propagateComposedEvents :true
                onPressed: {
                    triggered()
                    mouse.accepted=false
                }
            }
        }
    }

    Component{  //复选框类型
        id:checkComponent
        Rectangle{
            color: controlRoot.enabled?(checkMouseArea.containsMouse?parentAdMenu.itemHoveredBackColor:parentAdMenu.itemNormalBackColor):parentAdMenu.itemDisabledBackColor
            border.color:controlRoot.enabled?(checkMouseArea.containsMouse?parentAdMenu.itemHoveredBorderColor:parentAdMenu.itemNormalBorderColor):parentAdMenu.itemDisabledBorderColor
            CheckBox{
                id:checkBox
                width:controlRoot.height*0.7
                height: controlRoot.height*0.7
                anchors.left: parent.left
                anchors.leftMargin: 5
                anchors.verticalCenter: parent.verticalCenter
                hoverEnabled: false
                enabled: false
                checked: controlRoot.checked
                text: ""
                palette.window: controlRoot.enabled?(checkMouseArea.containsMouse?parentAdMenu.itemHoveredIndicatorColor:parentAdMenu.itemNormalIndicatorColor):parentAdMenu.itemDisabledIndicatorColor
                palette.text:  controlRoot.enabled?(checkMouseArea.containsMouse?parentAdMenu.itemHoveredIndicatorColor:parentAdMenu.itemNormalIndicatorColor):parentAdMenu.itemDisabledIndicatorColor
                palette.base: "transparent"
            }
            Text{
                anchors.left: checkBox.right
                anchors.leftMargin: 10
                width: parent.width-checkBox.width-15
                height: parent.height
                text: controlRoot.text
                color:  controlRoot.enabled?(checkMouseArea.containsMouse?parentAdMenu.itemHoveredTextColor:parentAdMenu.itemNormalTextColor):parentAdMenu.itemDisabledTextColor
                verticalAlignment: Qt.AlignVCenter
                font.pixelSize: parent.height/2
                textFormat: Text.RichText
            }
            MouseArea{
                id:checkMouseArea
                enabled: true
                hoverEnabled: true
                anchors.fill: parent
                propagateComposedEvents :true
                onPressed: {
                    controlRoot.checked=!controlRoot.checked
                    triggered()
                    mouse.accepted=false
                }
            }
        }
    }

    Component{  //单选类型
        id:raidoComponent
        Rectangle{
            color: controlRoot.enabled?(radioMouseArea.containsMouse?parentAdMenu.itemHoveredBackColor:parentAdMenu.itemNormalBackColor):parentAdMenu.itemDisabledBackColor
            border.color:controlRoot.enabled?(radioMouseArea.containsMouse?parentAdMenu.itemHoveredBorderColor:parentAdMenu.itemNormalBorderColor):parentAdMenu.itemDisabledBorderColor
            RadioButton{
                id:radioButton
                width:controlRoot.height*0.7
                height: controlRoot.height*0.7
                anchors.left: parent.left
                anchors.leftMargin: 5
                anchors.verticalCenter: parent.verticalCenter
                text: ""
                ButtonGroup.group: buttonGroup
                hoverEnabled: false
                enabled: false
                checked: controlRoot.checked
                checkable: true
                palette.window: controlRoot.enabled?(radioMouseArea.containsMouse?parentAdMenu.itemHoveredIndicatorColor:parentAdMenu.itemNormalIndicatorColor):parentAdMenu.itemDisabledIndicatorColor
                palette.text:  controlRoot.enabled?(radioMouseArea.containsMouse?parentAdMenu.itemHoveredIndicatorColor:parentAdMenu.itemNormalIndicatorColor):parentAdMenu.itemDisabledIndicatorColor
                palette.base: "transparent"
            }
            Text{
                anchors.left: radioButton.right
                anchors.leftMargin: 10
                width: parent.width-radioButton.width-15
                height: parent.height
                text: controlRoot.text
                color:  controlRoot.enabled?(radioMouseArea.containsMouse?parentAdMenu.itemHoveredTextColor:parentAdMenu.itemNormalTextColor):parentAdMenu.itemDisabledTextColor
                verticalAlignment: Qt.AlignVCenter
                font.pixelSize: parent.height/2
                textFormat: Text.RichText
            }
            MouseArea{
                id:radioMouseArea
                enabled: true
                hoverEnabled: true
                anchors.fill: parent
                propagateComposedEvents :true
                onPressed: {
                    controlRoot.checked=!controlRoot.checked
                    triggered()
                    mouse.accepted=false   
                }
            }
        }
    }
}
